#!/bin/sh

MPIRUN=`which mpirun`

if [ x"${QP_JOBFILENAME}" = x -o x"${QP_HOSTS_OMPI}" = x ]; then
    ${MPIRUN} $*
    exit $?
fi

MYARGS=$*
WORKDIR=`dirname ${QP_JOBFILENAME}`
MACHFILE=${WORKDIR}/mpi_machines.${QP_JOBID}

T=`echo "${MYARGS}" | grep -- -machinefile |wc -l`
if [ $T -gt 0 ]; then
    echo "Error:  Do not provide the machinefile for mpirun."
    echo "        It is generated automatically for you."
    exit -3
fi

# Make the Open MPI machine file
echo "${QP_HOSTS_OMPI}" > ${MACHFILE}

${MPIRUN} -machinefile ${MACHFILE} -mca plm_rsh_agent qptask -mca plm_rsh_args -x ${MYARGS}
ret=$?

rm -rf ${MACHFILE}

exit $ret
